package com.graduate.controller;

import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.graduate.common.shiro.PasswordHash;
import com.graduate.model.SysUser;
import com.graduate.model.SysUserRole;
import com.graduate.service.ISysUserRoleService;
import com.graduate.service.ISysUserService;
import com.graduate.util.Constant;
import com.graduate.util.ResponseResult;
import com.graduate.util.StringUtils;

@Controller
@RequestMapping(value="/register")
public class RegisterController extends BaseController{
	@Autowired
	private PasswordHash passwordHash;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ISysUserRoleService userRoleService;
	/**
	 * 注册页面
	 * @return
	 */
	@RequestMapping(value="/regist")
	public String regist() {
			return "register";
	}
	/**
	 * 注册页面表单注册用户号验证
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/checkUserCode")
	@ResponseBody
	public ResponseResult checkUserCode(String userCode) {
			SysUser user = new SysUser();
			user.setUserCode(userCode);
			SysUser exsitedUser = sysUserService.selectOne(user);
			if (exsitedUser != null) {
				return ResponseResult.createByFailedCode();
			}
			return ResponseResult.createBySuccessCode();
	}
	
	/**
	 * 注册页面表单注册用户名验证
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/checkUserName")
	@ResponseBody
	public ResponseResult checkUserName(String userName) {
		SysUser user=new SysUser();
		user.setUserName(userName);
			SysUser exsitedUser = sysUserService.selectOne(user);
			if (exsitedUser != null) {
				return ResponseResult.createByFailedCode();
			}
			return ResponseResult.createBySuccessCode();
	}
	
	
	/**
	 * 注册事件
	 * @param map
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/doRegister", method=RequestMethod.POST)
	@ResponseBody
	public ResponseResult doRegister(@RequestParam Map<String, Object> map, HttpServletRequest request) {
		String userCode = (String)map.get("userCode");
		String userName = (String)map.get("userName");
		String userType = (String)map.get("userType");
		String password = (String)map.get("password");		
		logger.info("开始注册事件");
		SysUser user = new SysUser();	
		user.setUserCode(userCode);
		user.setUserName(userName);
		SysUser exsitedUser = sysUserService.selectOne(user);
		if (exsitedUser != null) {
			return ResponseResult.createByFailedCode();
		}else{
		//设置id用uuid
		String uuid = UUID.randomUUID().toString();
		String id=uuid.replaceAll("-", ""); //去掉"-"符号
		user.setId(id);
		String salt = StringUtils.getRandomString(Constant.SALT_LENGTH);
		user.setPwd(passwordHash.toHex(password, salt));
		user.setSalt(salt);
		user.setStatus(1);
	    boolean flag1=sysUserService.insert(user);
		SysUserRole sur = new SysUserRole();
		sur.setUserId(user.getId());		
		sur.setRoleId(userType);
		boolean flag2=userRoleService.insert(sur);     //sys_user_role表分配权限
		if(flag1&&flag2){
			return ResponseResult.createBySuccessCode();
		}else{
			return ResponseResult.createByFailedCode();
		 }
		}
	}
}
